package sort;

import static sort.Common.printArray;

public class InsertSort {
    public static void insertSort(int[] nums) {
        int len = nums.length;
        for (int i = 1; i < len; ++i) {
            int tmp = nums[i];
            int j = i;
            while (j > 0 && tmp < nums[j - 1]) {
                nums[j] = nums[j - 1];
                j--;
            }
            if (j != i) {
                nums[j] = tmp;
            }
        }
    }

    public static void main(String[] args) {
        int[] nums = {5, 8, 3, 6, 2, 4, 1, 9, 7};
        printArray(nums);
        insertSort(nums);
        printArray(nums);
    }
}
